#include "StdAfx.h"
#include ".\xmlhelper.h"

XmlHelper::XmlHelper(void)
{
}

XmlHelper::~XmlHelper(void)
{
}



bool  XmlHelper::GetAttrib(MSXML2::IXMLDOMElementPtr elem,LPCTSTR propName, CString& propValue,CString defValue)
{
		propValue=defValue;

		MSXML2::IXMLDOMNodePtr node=elem->selectSingleNode(propName);
		if (node==NULL)
			return false;
		propValue=(LPCTSTR)node->Gettext();

		/*propValue.Replace("@r","\r");
		propValue.Replace("@n","\n");
		*/

		propValue.Replace("&amp;","&");
		propValue.Replace("&lt;","<");
		propValue.Replace("&gt;",">");
		propValue.Replace("&quot;","\"");
		propValue.Replace("&apos;","'");
		propValue.Replace("&rrr","\r");
		propValue.Replace("&nnn","\n");
		propValue.Replace("&nbsp;"," ");


		return true;
}


bool  XmlHelper::GetAttrib(MSXML2::IXMLDOMElementPtr elem,LPCTSTR propName, int& propValue,int defValue)
{
		propValue=defValue;

		MSXML2::IXMLDOMNodePtr node=elem->selectSingleNode(propName);
		if (node==NULL)
			return false;
		
		propValue=atoi((LPCTSTR)node->Gettext());
		    
		
		return true;

}


bool  XmlHelper::SetAttrib(MSXML2::IXMLDOMElementPtr elem,LPCTSTR propName,CString propValue)
{

	CString  str=propValue;

	/*str.Replace("\r","@r");
	str.Replace("\n","@n");*/

	str.Replace("&","&amp;");
	str.Replace("<","&lt;");
	str.Replace(">","&gt;");
	str.Replace("\"","&quot;");
	str.Replace("'","&apos;");
	str.Replace("\r","&rrr");
	str.Replace("\n","&nnn");
	str.Replace(" ","&nbsp;");



	HRESULT hr=elem->setAttribute(propName,(LPCTSTR)str);

	return  (hr==S_OK);
}



MSXML2::IXMLDOMElementPtr  XmlHelper::CreateChildNode(MSXML2::IXMLDOMDocumentPtr doc,MSXML2::IXMLDOMElementPtr elem,LPCTSTR nodeName,LPCTSTR nodeValue)
{
	MSXML2::IXMLDOMElementPtr childElem=doc->createElement(nodeName);

	if (childElem==NULL)
		return NULL;


	CString  str=nodeValue;

	/*str.Replace("\r","@r");
	str.Replace("\n","@n");*/

	str.Replace("&","&amp;");
	str.Replace("<","&lt;");
	str.Replace(">","&gt;");
	str.Replace("\"","&quot;");
	str.Replace("'","&apos;");
	str.Replace("\r","&rrr");
	str.Replace("\n","&nnn");
	str.Replace(" ","&nbsp;");



	childElem->Puttext((LPCTSTR)str);

	MSXML2::IXMLDOMNodePtr node=childElem;
	elem->appendChild(node);

	return childElem;
}


MSXML2::IXMLDOMElementPtr XmlHelper::GetChildNode(MSXML2::IXMLDOMElementPtr elem,LPCTSTR nodeName,CString& nodeValue)
{
	MSXML2::IXMLDOMNodePtr node=elem->selectSingleNode(nodeName);
	if (node==NULL)
		return NULL;
	
	MSXML2::IXMLDOMElementPtr childItem=node;
	
	nodeValue=(LPCTSTR)node->Gettext();


	/*nodeValue.Replace("@r","\r");
	nodeValue.Replace("@n","\n");*/

	nodeValue.Replace("&amp;","&");
	nodeValue.Replace("&lt;","<");
	nodeValue.Replace("&gt;",">");
	nodeValue.Replace("&quot;","\"");
	nodeValue.Replace("&apos;","'");
	nodeValue.Replace("&rrr","\r");
	nodeValue.Replace("&nnn","\n");
	nodeValue.Replace("&nbsp;"," ");



	return childItem;
}




bool   XmlHelper::SetAttrib(MSXML2::IXMLDOMElementPtr elem,LPCTSTR propName,BYTE* lpByte,int count)
{
	_variant_t var;

	var.vt= VT_UI1 | VT_ARRAY ;

	SAFEARRAYBOUND bound;
	bound.cElements = count;
	bound.lLbound =0 ;
	
	var.parray = ::SafeArrayCreate(VT_UI1, 1, &bound);
	if (var.parray)
	{
		void* pDest;
	    ::SafeArrayAccessData(var.parray, &pDest);
		memcpy(pDest, lpByte, count);
		::SafeArrayUnaccessData(var.parray);
	}

	HRESULT hr=elem->setAttribute(propName,var);
	return (hr==S_OK);
}


bool  XmlHelper::GetAttrib(MSXML2::IXMLDOMElementPtr elem,LPCTSTR propName,BYTE* lpByte,int maxCount)
{
	MSXML2::IXMLDOMNodePtr node=elem->selectSingleNode(propName);
	
	if (node==NULL)
		return false;

	_variant_t var=node->GetnodeValue();

	if (!var.parray)
		return false;

	SAFEARRAYBOUND *bound;

	bound=var.parray->rgsabound; 

	int count=bound->cElements;

	void* pDest;
    ::SafeArrayAccessData(var.parray, &pDest);
	memcpy(lpByte,pDest, min(count,maxCount));
	::SafeArrayUnaccessData(var.parray);

	return TRUE;
}



